# Maintainer: David Runge <dvzrv@archlinux.org>

_name=JUCE
pkgbase=juce
pkgname=(juce{,-docs})
pkgver=7.0.9
pkgrel=1
pkgdesc="Cross-platform C++ framework"
arch=(loong64 x86_64)
url="https://github.com/juce-framework/juce/"
license=(
  Apache-2.0
  BSD-3-Clause
  GPL-3.0-only
  ISC
  Zlib
)
makedepends=(
  alsa-lib
  cmake
  freetype2
  doxygen
  flac
  gendesk
  graphviz
  gtk3
  jack
  ladspa
  libjpeg-turbo
  libpng
  libvorbis
  python
  webkit2gtk
  zlib
)
source=(
  $pkgname-$pkgver.tar.gz::https://github.com/juce-framework/$pkgname/archive/$pkgver.tar.gz
  $pkgname-6.1.2-cmake_install.patch
  $pkgname-6.1.2-cmake_juce_utils.patch
  $pkgname-6.1.3-cmake_link_against_system_deps.patch
  $pkgname-6.1.2-devendor_libs.patch
  $pkgname-6.1.2-projucer_disable_update_check.patch
  $pkgname-6.1.2-projucer_juce_paths.patch
  ${pkgname}_VSTInterface.h
)
sha512sums=('e8519ca4ee55f126e14d7c927e2f36e7c30117a34da84f2b4e730afcea4a864b07455ad3f89d751fa47c8c155548b0c3150a667f1a9d199e582eadd146b6c697'
            '52aba043ad3e5154b67f4d140f1d1e936962325763b47bbec57e1ef0a9cccbe6fa0b99598a756cb76853b5c1c5084b32055fc6a76f992b10323d867c82023399'
            '0cad32e728e40f59565f57a9d0369db5388f11fb62412a18950fc31f7cf2be679917c143e0d073b6f9a3728e13a3040dc11b18713657c90a0c9c12d60e3ab2a4'
            '8d5105e7ea0977cba598c13e53708ca88cdea52c3fc92cca8849eb4e64209d4902d8eef6a1bda0894138f2e1fe4247156b1d39e74207d978534768f92490f2e8'
            'ccec528a1daaf255a4686dad2d59314adab03a935a79893b04a8f04256179a2b1ba84fa563fd6781b314b9d2fc1ddf4892576f77d748e40577747013b3a60bc3'
            '2e1b823d9e5611e2c9efa1459f309f9add76d163411bb32cbd8c93f9751c6b97842af430f866f9ebce02d7602a655fd1f9d62f0530e5dc4d2621cbbd5444aad8'
            '766b761bf60a09fd01e9e6422df5b34c5305d41672b82b2c54819c581a952af5d7c8c39868c8615b57b14f772624bae4545ec878f2098ab5b8afb2ecf59146a1'
            '36a244740872cd203db3e1cebba5a5e245a3300a8c6c76046455ecc38083348f069987d0cba8f569390b4f0ba855799b8ff559cb540f02382b67a46d5b9cc420')
b2sums=('1b6c42276b8cd08453f81a8463bdcea523d150988052d1c1beb81b6cc40831c4255e89024d39f5f768a84ad56feb16a63a3c6069c7805a30687ef815fabe9d00'
        '2419fd707b03a5e33cef03576cbeddb99ab4fd67f4fbec8047668ffb1daf78803cd608d7255893394e6d83dce5afd51608793384567228130a0082bcc9a67f60'
        '56434fb5e2ed1db25963b8046963edcf5b0074d1f52810bb57f0348b6d38251b3c976683f1450399646d2999082f953d60416f2871d43bc6cc4d76edc9ab34d7'
        'cc766ff15858ed1d66c4f79b182903931e9bcfb0c83ab7ef5fccb487185b9ea31284764c232df962b1438671d79710f9a8d5a0e1d36ffeea43fca23b446c80e8'
        'ee72102ecdf57490e9aec0fe6368d61e3c9f86a8b13fd35b3889eac591d716cc98c19b87d6d09360848202b4a108bbc39639d36e28d69908c80ee514b2cf40ee'
        'cd6c59deed414529c49dbf8506905cc7848bb48073e054d4cf2450c17c7268860330bd2d2e6d30e5a2a68cec2106e3498619e85ce602439f75ba9393084498f6'
        '4d42e73b4d15f0ef16a55d927de43127518f4420fe54f78b2c2a0e57a08eb192138f65f381017d0f1e6ca286009f63f16c269b58e7405c35894c0bc3ac91de7b'
        'f56e59984bde66d482fdeae3cefc0d4dc9e25c9c170f2f0b46ec53bd63ec72432b1764ff282ef8093c1029a427852c09087827d22023b526786a03977cbb7a32')

prepare() {
  gendesk \
    -n \
    --pkgname "com.juce_projucer" \
    --name "Projucer" \
    --pkgdesc "Cross-platform project manager and C++ code editor" \
    --exec "Projucer" \
    --icon "Projucer" \
    --genericname "Code manager and editor" \
    --categories "Development"

  cd $_name-$pkgver
  for _patch in ../*.patch; do
    printf "Applying patch %s\n" "${_patch}"
    patch -Np1 -i "${_patch}"
  done

  # remove bundled sources for flac, libogg, vorbis, libjpeg, libpng and zlib
  # as we are linking against system libs
  rm -rvf modules/juce_audio_formats/codecs/flac/ \
    modules/juce_audio_formats/codecs/oggvorbis/ \
    modules/juce_audio_plugin_client/AU/ \
    modules/juce_graphics/image_formats/jpglib/ \
    modules/juce_graphics/image_formats/pnglib/ \
    modules/juce_core/zip/zlib/
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D JUCE_BUILD_EXTRAS=ON
    -D JUCE_TOOL_INSTALL_DIR=bin
    -D JUCER_ENABLE_GPL_MODE=1
    -S $_name-$pkgver
    -W no-dev
  )
  cmake "${cmake_options[@]}"
  cmake --build build --verbose
  make -C $_name-$pkgver/docs/doxygen
}

package_juce() {
  depends=(
    freetype2 libfreetype.so
    gcc-libs
    glibc
    hicolor-icon-theme
    libpng
    libjpeg-turbo libjpeg.so
    ttf-font
    zlib
  )
  optdepends=(
    'python: for building LV2 plugins'
  )

  make DESTDIR="$pkgdir" VERBOSE=1 -C build install
  # projucer has no install target
  install -vDm 755 build/extras/Projucer/Projucer_artefacts/None/Projucer -t "$pkgdir/usr/bin/"
  # install custom vst2 handling from juce < 5.4.1
  install -vDm 644 juce_VSTInterface.h -t "$pkgdir/usr/share/juce/modules/juce_audio_processors/format_types/"
  # xdg desktop integration
  install -vDm 644 *.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 $_name-$pkgver/examples/Assets/juce_icon.png "$pkgdir/usr/share/icons/hicolor/512x512/apps/Projucer.png"
  # docs
  install -vDm 644 $_name-$pkgver/{BREAKING_CHANGES,CHANGE_LIST,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
  # license
  install -vDm 644 $_name-$pkgver/LICENSE.md -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_juce-docs() {
  (
    cd $_name-$pkgver/docs/doxygen/doc
    find . -type f -exec install -vDm 644 {} "$pkgdir/usr/share/doc/$pkgbase/html/"{} \;
  )
  # license
  install -vDm 644 $_name-$pkgver/LICENSE.md -t "$pkgdir/usr/share/licenses/$pkgname/"
}
